package chat.fragments;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.orm.SugarRecord;

import java.util.ArrayList;
import java.util.List;

import chat.activities.ChatActivity;
import chat.adapter.UserAdapter;
import chat.network.http.GetAllContacts;
import inter.Delegate;
import inter.ResponseStatus;
import model.User;


/**
 * 用户和分组信息
 * Created by 凯 on 2015/10/28.
 */
public class UserFragment extends Fragment {

    ListView lv_users;

    PullToRefreshListView pullToRefreshListView;

    private List<User> usersSubs = new ArrayList<User>();
    UserAdapter userAdapter = new UserAdapter();
    {
        userAdapter.setUsersSubs(usersSubs);
    }


    GetAllContacts getAllContacts = new GetAllContacts();
    Delegate GetAllContactsResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            pullToRefreshListView.onRefreshComplete();
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetAllContacts.ResponseContacts responseContacts = gson.fromJson(response,GetAllContacts.ResponseContacts.class);
            if (responseContacts.getStatus().equals(ResponseStatus.SUCCESS))
            {
                SugarRecord.deleteAll(User.class);
                for(GetAllContacts.ContactsGroup contactsGroup:responseContacts.getUsersSubs())
                {
                    User user = new User();
                    user.setUser_ID(contactsGroup.getUser_ID());
                    user.setUser_RealName(contactsGroup.getUser_RealName());
                    user.setUser_Group(contactsGroup.getUser_Group());
                    user.setUser_NickName(contactsGroup.getUser_NickName());
                    user.setUser_Status(contactsGroup.getUser_Status());
                    SugarRecord.save(user);
                }

                getUsersFromDb(usersSubs);
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            pullToRefreshListView.onRefreshComplete();
        }
    };

    {
        getAllContacts.setDelegate(GetAllContactsResponse);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_friend_im, null);
        initview(view);
        initdata();
        return view;
    }

    private void initdata() {
       getUsersFromDb(usersSubs);
    }

    // 初始化view
    private void initview(View view) {

        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.id_lv_refreshlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lv_users = pullToRefreshListView.getRefreshableView();
        lv_users.setAdapter(userAdapter);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                getAllContacts.get();
            }
        });

        lv_users.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                User user = usersSubs.get(position-1);
                Intent intent = new Intent(getActivity(),ChatActivity.class);
                intent.putExtra("user",user);
                getActivity().startActivity(intent);
            }
        });
    }

    // 从本地获取user数据
    public void getUsersFromDb(List<User> users)
    {
        List<User> tmp = SugarRecord.listAll(User.class);
        users.clear();
        users.addAll(tmp);
        userAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("resume", "重新显示");
        getAllContacts.setUser_ID(MyApplication.getOwnUser().getUser_ID());
        getAllContacts.get();

    }
}
